{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "from sklearn.model_selection import train_test_split"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>instant</th>\n",
       "      <th>season_1</th>\n",
       "      <th>season_2</th>\n",
       "      <th>season_3</th>\n",
       "      <th>season_4</th>\n",
       "      <th>mnth_1</th>\n",
       "      <th>mnth_2</th>\n",
       "      <th>mnth_3</th>\n",
       "      <th>mnth_4</th>\n",
       "      <th>mnth_5</th>\n",
       "      <th>...</th>\n",
       "      <th>weekday_5</th>\n",
       "      <th>weekday_6</th>\n",
       "      <th>temp</th>\n",
       "      <th>atemp</th>\n",
       "      <th>hum</th>\n",
       "      <th>windspeed</th>\n",
       "      <th>holiday</th>\n",
       "      <th>workingday</th>\n",
       "      <th>yr</th>\n",
       "      <th>cnt</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0.355170</td>\n",
       "      <td>0.373517</td>\n",
       "      <td>0.828620</td>\n",
       "      <td>0.284606</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>985</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0.379232</td>\n",
       "      <td>0.360541</td>\n",
       "      <td>0.715771</td>\n",
       "      <td>0.466215</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>801</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0.171000</td>\n",
       "      <td>0.144830</td>\n",
       "      <td>0.449638</td>\n",
       "      <td>0.465740</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1349</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0.175530</td>\n",
       "      <td>0.174649</td>\n",
       "      <td>0.607131</td>\n",
       "      <td>0.284297</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1562</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0.209120</td>\n",
       "      <td>0.197158</td>\n",
       "      <td>0.449313</td>\n",
       "      <td>0.339143</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1600</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 35 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "   instant  season_1  season_2  season_3  season_4  mnth_1  mnth_2  mnth_3  \\\n",
       "0        1         1         0         0         0       1       0       0   \n",
       "1        2         1         0         0         0       1       0       0   \n",
       "2        3         1         0         0         0       1       0       0   \n",
       "3        4         1         0         0         0       1       0       0   \n",
       "4        5         1         0         0         0       1       0       0   \n",
       "\n",
       "   mnth_4  mnth_5  ...   weekday_5  weekday_6      temp     atemp       hum  \\\n",
       "0       0       0  ...           0          1  0.355170  0.373517  0.828620   \n",
       "1       0       0  ...           0          0  0.379232  0.360541  0.715771   \n",
       "2       0       0  ...           0          0  0.171000  0.144830  0.449638   \n",
       "3       0       0  ...           0          0  0.175530  0.174649  0.607131   \n",
       "4       0       0  ...           0          0  0.209120  0.197158  0.449313   \n",
       "\n",
       "   windspeed  holiday  workingday  yr   cnt  \n",
       "0   0.284606        0           0   0   985  \n",
       "1   0.466215        0           0   0   801  \n",
       "2   0.465740        0           1   0  1349  \n",
       "3   0.284297        0           1   0  1562  \n",
       "4   0.339143        0           1   0  1600  \n",
       "\n",
       "[5 rows x 35 columns]"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 读入数据\n",
    "data = pd.read_csv(\"FE_day.csv\")\n",
    "data.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(584, 35)\n",
      "(147, 35)\n"
     ]
    }
   ],
   "source": [
    "train,test = train_test_split(data,test_size=0.2,random_state=42)\n",
    "print(train.shape)\n",
    "print(test.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>instant</th>\n",
       "      <th>season_1</th>\n",
       "      <th>season_2</th>\n",
       "      <th>season_3</th>\n",
       "      <th>season_4</th>\n",
       "      <th>mnth_1</th>\n",
       "      <th>mnth_2</th>\n",
       "      <th>mnth_3</th>\n",
       "      <th>mnth_4</th>\n",
       "      <th>mnth_5</th>\n",
       "      <th>...</th>\n",
       "      <th>weekday_5</th>\n",
       "      <th>weekday_6</th>\n",
       "      <th>temp</th>\n",
       "      <th>atemp</th>\n",
       "      <th>hum</th>\n",
       "      <th>windspeed</th>\n",
       "      <th>holiday</th>\n",
       "      <th>workingday</th>\n",
       "      <th>yr</th>\n",
       "      <th>cnt</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>682</th>\n",
       "      <td>683</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0.354130</td>\n",
       "      <td>0.320487</td>\n",
       "      <td>0.681663</td>\n",
       "      <td>0.658984</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>4094</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>250</th>\n",
       "      <td>251</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0.716207</td>\n",
       "      <td>0.625197</td>\n",
       "      <td>0.966134</td>\n",
       "      <td>0.351198</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1842</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>336</th>\n",
       "      <td>337</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0.299097</td>\n",
       "      <td>0.303920</td>\n",
       "      <td>0.630249</td>\n",
       "      <td>0.151301</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>3614</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>260</th>\n",
       "      <td>261</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0.558691</td>\n",
       "      <td>0.540107</td>\n",
       "      <td>0.714653</td>\n",
       "      <td>0.321790</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>4274</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>543</th>\n",
       "      <td>544</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0.795440</td>\n",
       "      <td>0.737336</td>\n",
       "      <td>0.370180</td>\n",
       "      <td>0.514117</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>7335</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 35 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "     instant  season_1  season_2  season_3  season_4  mnth_1  mnth_2  mnth_3  \\\n",
       "682      683         0         0         0         1       0       0       0   \n",
       "250      251         0         0         1         0       0       0       0   \n",
       "336      337         0         0         0         1       0       0       0   \n",
       "260      261         0         0         1         0       0       0       0   \n",
       "543      544         0         0         1         0       0       0       0   \n",
       "\n",
       "     mnth_4  mnth_5  ...   weekday_5  weekday_6      temp     atemp       hum  \\\n",
       "682       0       0  ...           0          0  0.354130  0.320487  0.681663   \n",
       "250       0       0  ...           0          0  0.716207  0.625197  0.966134   \n",
       "336       0       0  ...           0          1  0.299097  0.303920  0.630249   \n",
       "260       0       0  ...           0          0  0.558691  0.540107  0.714653   \n",
       "543       0       0  ...           0          0  0.795440  0.737336  0.370180   \n",
       "\n",
       "     windspeed  holiday  workingday  yr   cnt  \n",
       "682   0.658984        0           1   1  4094  \n",
       "250   0.351198        0           1   0  1842  \n",
       "336   0.151301        0           0   0  3614  \n",
       "260   0.321790        0           0   0  4274  \n",
       "543   0.514117        0           1   1  7335  \n",
       "\n",
       "[5 rows x 35 columns]"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "train.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>instant</th>\n",
       "      <th>season_1</th>\n",
       "      <th>season_2</th>\n",
       "      <th>season_3</th>\n",
       "      <th>season_4</th>\n",
       "      <th>mnth_1</th>\n",
       "      <th>mnth_2</th>\n",
       "      <th>mnth_3</th>\n",
       "      <th>mnth_4</th>\n",
       "      <th>mnth_5</th>\n",
       "      <th>...</th>\n",
       "      <th>weekday_5</th>\n",
       "      <th>weekday_6</th>\n",
       "      <th>temp</th>\n",
       "      <th>atemp</th>\n",
       "      <th>hum</th>\n",
       "      <th>windspeed</th>\n",
       "      <th>holiday</th>\n",
       "      <th>workingday</th>\n",
       "      <th>yr</th>\n",
       "      <th>cnt</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>703</th>\n",
       "      <td>704</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0.519232</td>\n",
       "      <td>0.511907</td>\n",
       "      <td>0.754499</td>\n",
       "      <td>0.312814</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>6606</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>33</th>\n",
       "      <td>34</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0.159278</td>\n",
       "      <td>0.129699</td>\n",
       "      <td>0.450207</td>\n",
       "      <td>0.526439</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1550</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>300</th>\n",
       "      <td>301</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0.338555</td>\n",
       "      <td>0.314694</td>\n",
       "      <td>0.602399</td>\n",
       "      <td>0.426921</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>3747</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>456</th>\n",
       "      <td>457</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0.456929</td>\n",
       "      <td>0.443956</td>\n",
       "      <td>0.695373</td>\n",
       "      <td>0.308976</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>6041</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>633</th>\n",
       "      <td>634</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0.611648</td>\n",
       "      <td>0.610519</td>\n",
       "      <td>0.586118</td>\n",
       "      <td>0.441027</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>7538</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 35 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "     instant  season_1  season_2  season_3  season_4  mnth_1  mnth_2  mnth_3  \\\n",
       "703      704         0         0         0         1       0       0       0   \n",
       "33        34         1         0         0         0       0       1       0   \n",
       "300      301         0         0         0         1       0       0       0   \n",
       "456      457         0         1         0         0       0       0       0   \n",
       "633      634         0         0         0         1       0       0       0   \n",
       "\n",
       "     mnth_4  mnth_5  ...   weekday_5  weekday_6      temp     atemp       hum  \\\n",
       "703       0       0  ...           0          0  0.519232  0.511907  0.754499   \n",
       "33        0       0  ...           0          0  0.159278  0.129699  0.450207   \n",
       "300       0       0  ...           1          0  0.338555  0.314694  0.602399   \n",
       "456       1       0  ...           0          0  0.456929  0.443956  0.695373   \n",
       "633       0       0  ...           0          0  0.611648  0.610519  0.586118   \n",
       "\n",
       "     windspeed  holiday  workingday  yr   cnt  \n",
       "703   0.312814        0           1   1  6606  \n",
       "33    0.526439        0           1   0  1550  \n",
       "300   0.426921        0           1   0  3747  \n",
       "456   0.308976        0           0   1  6041  \n",
       "633   0.441027        0           1   1  7538  \n",
       "\n",
       "[5 rows x 35 columns]"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "train_X,train_y = train.loc[:,'season_1':'yr'],train.loc[:,'cnt']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>season_1</th>\n",
       "      <th>season_2</th>\n",
       "      <th>season_3</th>\n",
       "      <th>season_4</th>\n",
       "      <th>mnth_1</th>\n",
       "      <th>mnth_2</th>\n",
       "      <th>mnth_3</th>\n",
       "      <th>mnth_4</th>\n",
       "      <th>mnth_5</th>\n",
       "      <th>mnth_6</th>\n",
       "      <th>...</th>\n",
       "      <th>weekday_4</th>\n",
       "      <th>weekday_5</th>\n",
       "      <th>weekday_6</th>\n",
       "      <th>temp</th>\n",
       "      <th>atemp</th>\n",
       "      <th>hum</th>\n",
       "      <th>windspeed</th>\n",
       "      <th>holiday</th>\n",
       "      <th>workingday</th>\n",
       "      <th>yr</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>682</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0.354130</td>\n",
       "      <td>0.320487</td>\n",
       "      <td>0.681663</td>\n",
       "      <td>0.658984</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>250</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0.716207</td>\n",
       "      <td>0.625197</td>\n",
       "      <td>0.966134</td>\n",
       "      <td>0.351198</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>336</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0.299097</td>\n",
       "      <td>0.303920</td>\n",
       "      <td>0.630249</td>\n",
       "      <td>0.151301</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>260</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0.558691</td>\n",
       "      <td>0.540107</td>\n",
       "      <td>0.714653</td>\n",
       "      <td>0.321790</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>543</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0.795440</td>\n",
       "      <td>0.737336</td>\n",
       "      <td>0.370180</td>\n",
       "      <td>0.514117</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 33 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "     season_1  season_2  season_3  season_4  mnth_1  mnth_2  mnth_3  mnth_4  \\\n",
       "682         0         0         0         1       0       0       0       0   \n",
       "250         0         0         1         0       0       0       0       0   \n",
       "336         0         0         0         1       0       0       0       0   \n",
       "260         0         0         1         0       0       0       0       0   \n",
       "543         0         0         1         0       0       0       0       0   \n",
       "\n",
       "     mnth_5  mnth_6 ...  weekday_4  weekday_5  weekday_6      temp     atemp  \\\n",
       "682       0       0 ...          0          0          0  0.354130  0.320487   \n",
       "250       0       0 ...          1          0          0  0.716207  0.625197   \n",
       "336       0       0 ...          0          0          1  0.299097  0.303920   \n",
       "260       0       0 ...          0          0          0  0.558691  0.540107   \n",
       "543       0       1 ...          0          0          0  0.795440  0.737336   \n",
       "\n",
       "          hum  windspeed  holiday  workingday  yr  \n",
       "682  0.681663   0.658984        0           1   1  \n",
       "250  0.966134   0.351198        0           1   0  \n",
       "336  0.630249   0.151301        0           0   0  \n",
       "260  0.714653   0.321790        0           0   0  \n",
       "543  0.370180   0.514117        0           1   1  \n",
       "\n",
       "[5 rows x 33 columns]"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "train_X.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "682    4094\n",
       "250    1842\n",
       "336    3614\n",
       "260    4274\n",
       "543    7335\n",
       "Name: cnt, dtype: int64"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "train_y.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_X,test_y = test.loc[:,'season_1':'yr'],test.loc[:,'cnt']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>season_1</th>\n",
       "      <th>season_2</th>\n",
       "      <th>season_3</th>\n",
       "      <th>season_4</th>\n",
       "      <th>mnth_1</th>\n",
       "      <th>mnth_2</th>\n",
       "      <th>mnth_3</th>\n",
       "      <th>mnth_4</th>\n",
       "      <th>mnth_5</th>\n",
       "      <th>mnth_6</th>\n",
       "      <th>...</th>\n",
       "      <th>weekday_4</th>\n",
       "      <th>weekday_5</th>\n",
       "      <th>weekday_6</th>\n",
       "      <th>temp</th>\n",
       "      <th>atemp</th>\n",
       "      <th>hum</th>\n",
       "      <th>windspeed</th>\n",
       "      <th>holiday</th>\n",
       "      <th>workingday</th>\n",
       "      <th>yr</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>703</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0.519232</td>\n",
       "      <td>0.511907</td>\n",
       "      <td>0.754499</td>\n",
       "      <td>0.312814</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>33</th>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0.159278</td>\n",
       "      <td>0.129699</td>\n",
       "      <td>0.450207</td>\n",
       "      <td>0.526439</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>300</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0.338555</td>\n",
       "      <td>0.314694</td>\n",
       "      <td>0.602399</td>\n",
       "      <td>0.426921</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>456</th>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0.456929</td>\n",
       "      <td>0.443956</td>\n",
       "      <td>0.695373</td>\n",
       "      <td>0.308976</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>633</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0.611648</td>\n",
       "      <td>0.610519</td>\n",
       "      <td>0.586118</td>\n",
       "      <td>0.441027</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 33 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "     season_1  season_2  season_3  season_4  mnth_1  mnth_2  mnth_3  mnth_4  \\\n",
       "703         0         0         0         1       0       0       0       0   \n",
       "33          1         0         0         0       0       1       0       0   \n",
       "300         0         0         0         1       0       0       0       0   \n",
       "456         0         1         0         0       0       0       0       1   \n",
       "633         0         0         0         1       0       0       0       0   \n",
       "\n",
       "     mnth_5  mnth_6 ...  weekday_4  weekday_5  weekday_6      temp     atemp  \\\n",
       "703       0       0 ...          0          0          0  0.519232  0.511907   \n",
       "33        0       0 ...          1          0          0  0.159278  0.129699   \n",
       "300       0       0 ...          0          1          0  0.338555  0.314694   \n",
       "456       0       0 ...          0          0          0  0.456929  0.443956   \n",
       "633       0       0 ...          0          0          0  0.611648  0.610519   \n",
       "\n",
       "          hum  windspeed  holiday  workingday  yr  \n",
       "703  0.754499   0.312814        0           1   1  \n",
       "33   0.450207   0.526439        0           1   0  \n",
       "300  0.602399   0.426921        0           1   0  \n",
       "456  0.695373   0.308976        0           0   1  \n",
       "633  0.586118   0.441027        0           1   1  \n",
       "\n",
       "[5 rows x 33 columns]"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test_X.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "703    6606\n",
       "33     1550\n",
       "300    3747\n",
       "456    6041\n",
       "633    7538\n",
       "Name: cnt, dtype: int64"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test_y.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 最小二乘线性回归模型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "LinearRegression(copy_X=True, fit_intercept=True, n_jobs=1, normalize=False)"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#训练模型\n",
    "from sklearn.linear_model import LinearRegression\n",
    "# 使用默认配置初始化\n",
    "lr = LinearRegression(fit_intercept=True, normalize=False, copy_X=True, n_jobs=1)\n",
    "# 训练模型参数\n",
    "lr.fit(train_X, train_y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "pred_y = lr.predict(test_X)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.metrics import mean_squared_error"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "import math\n",
    "lr_result = math.sqrt(mean_squared_error(test_y, pred_y))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 岭回归模型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "GridSearchCV(cv=None, error_score='raise',\n",
       "       estimator=Ridge(alpha=1.0, copy_X=True, fit_intercept=True, max_iter=None,\n",
       "   normalize=False, random_state=None, solver='auto', tol=0.001),\n",
       "       fit_params=None, iid=True, n_jobs=1,\n",
       "       param_grid={'alpha': [0.001, 0.01, 0.1, 1, 10, 100, 1000]},\n",
       "       pre_dispatch='2*n_jobs', refit=True, return_train_score=True,\n",
       "       scoring='neg_mean_squared_error', verbose=0)"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from sklearn.linear_model import Ridge\n",
    "from sklearn.model_selection import GridSearchCV\n",
    "\n",
    "params={'alpha': [0.001,0.01,0.1,1,10,100,1000]}\n",
    "rdg_reg = Ridge()\n",
    "rr = GridSearchCV(rdg_reg,params, scoring = 'neg_mean_squared_error', return_train_score=True)\n",
    "rr.fit(train_X,train_y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'mean_fit_time': array([0.01413012, 0.00128643, 0.00126211, 0.00114195, 0.00118685,\n",
       "        0.00114497, 0.00114632]),\n",
       " 'std_fit_time': array([1.78604785e-02, 2.00044633e-04, 9.60810769e-05, 6.42797399e-06,\n",
       "        4.88368321e-05, 9.69503744e-06, 2.56260233e-05]),\n",
       " 'mean_score_time': array([0.00032123, 0.00024986, 0.00028046, 0.00029794, 0.00027124,\n",
       "        0.0002358 , 0.00023556]),\n",
       " 'std_score_time': array([1.11086571e-04, 1.85538067e-05, 1.52964095e-05, 3.04705619e-05,\n",
       "        7.48227583e-06, 3.20463898e-06, 8.92080638e-07]),\n",
       " 'param_alpha': masked_array(data=[0.001, 0.01, 0.1, 1, 10, 100, 1000],\n",
       "              mask=[False, False, False, False, False, False, False],\n",
       "        fill_value='?',\n",
       "             dtype=object),\n",
       " 'params': [{'alpha': 0.001},\n",
       "  {'alpha': 0.01},\n",
       "  {'alpha': 0.1},\n",
       "  {'alpha': 1},\n",
       "  {'alpha': 10},\n",
       "  {'alpha': 100},\n",
       "  {'alpha': 1000}],\n",
       " 'split0_test_score': array([ -661071.07762064,  -651415.18093335,  -625817.94412677,\n",
       "         -614389.08162828,  -686891.71296848, -1426579.05738538,\n",
       "        -3063050.82344423]),\n",
       " 'split1_test_score': array([ -657474.99861956,  -657445.3502835 ,  -657601.19127959,\n",
       "         -665278.25236985,  -747771.38878483, -1490897.763482  ,\n",
       "        -3232219.88403203]),\n",
       " 'split2_test_score': array([ -694026.66664379,  -693584.09310446,  -690228.71324573,\n",
       "         -676755.17392925,  -695697.88669031, -1327852.94961293,\n",
       "        -2980806.70596409]),\n",
       " 'mean_test_score': array([ -670817.90786941,  -667436.84529033,  -657827.22906492,\n",
       "         -652098.68813683,  -710145.02544489, -1415259.33611995,\n",
       "        -3092216.24814196]),\n",
       " 'std_test_score': array([ 16434.75235696,  18605.29844178,  26284.72765952,  27106.62909793,\n",
       "         26881.31250478,  67015.0136493 , 104652.18414028]),\n",
       " 'rank_test_score': array([4, 3, 2, 1, 5, 6, 7], dtype=int32),\n",
       " 'split0_train_score': array([ -556202.30808425,  -556278.87961851,  -557419.13827814,\n",
       "         -562423.35395971,  -638132.62413666, -1349284.72205222,\n",
       "        -3057004.57363249]),\n",
       " 'split1_train_score': array([ -539234.79038588,  -539241.02609054,  -539559.67967202,\n",
       "         -543818.01793544,  -611799.28300262, -1330290.97574544,\n",
       "        -2999632.04298625]),\n",
       " 'split2_train_score': array([ -532179.16516176,  -532186.48014889,  -532559.97841616,\n",
       "         -537958.16990257,  -617527.12512273, -1358610.66341973,\n",
       "        -3097390.69196661]),\n",
       " 'mean_train_score': array([ -542538.75454396,  -542568.79528598,  -543179.59878877,\n",
       "         -548066.51393257,  -622486.34408734, -1346062.1204058 ,\n",
       "        -3051342.43619512]),\n",
       " 'std_train_score': array([10081.83129321, 10113.24095479, 10466.5281754 , 10429.88003816,\n",
       "        11308.00906068, 11783.8886768 , 40110.12517961])}"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rr.cv_results_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'alpha': 1}"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rr.best_params_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "-652098.6881368348"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rr.best_score_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Ridge(alpha=1, copy_X=True, fit_intercept=True, max_iter=None,\n",
       "   normalize=False, random_state=None, solver='auto', tol=0.001)"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rr.best_estimator_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [],
   "source": [
    "pred_y = rr.predict(test_X)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [],
   "source": [
    "rr_result = math.sqrt(mean_squared_error(test_y, pred_y))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Lasso模型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/usr/bin/anaconda3/lib/python3.7/site-packages/sklearn/linear_model/coordinate_descent.py:491: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Fitting data with very small alpha may cause precision problems.\n",
      "  ConvergenceWarning)\n",
      "/usr/bin/anaconda3/lib/python3.7/site-packages/sklearn/linear_model/coordinate_descent.py:491: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Fitting data with very small alpha may cause precision problems.\n",
      "  ConvergenceWarning)\n",
      "/usr/bin/anaconda3/lib/python3.7/site-packages/sklearn/linear_model/coordinate_descent.py:491: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Fitting data with very small alpha may cause precision problems.\n",
      "  ConvergenceWarning)\n",
      "/usr/bin/anaconda3/lib/python3.7/site-packages/sklearn/linear_model/coordinate_descent.py:491: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Fitting data with very small alpha may cause precision problems.\n",
      "  ConvergenceWarning)\n",
      "/usr/bin/anaconda3/lib/python3.7/site-packages/sklearn/linear_model/coordinate_descent.py:491: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Fitting data with very small alpha may cause precision problems.\n",
      "  ConvergenceWarning)\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "GridSearchCV(cv=None, error_score='raise',\n",
       "       estimator=Lasso(alpha=1.0, copy_X=True, fit_intercept=True, max_iter=1000,\n",
       "   normalize=False, positive=False, precompute=False, random_state=None,\n",
       "   selection='cyclic', tol=0.0001, warm_start=False),\n",
       "       fit_params=None, iid=True, n_jobs=1,\n",
       "       param_grid={'alpha': [0.001, 0.01, 0.1, 1, 10, 100, 1000]},\n",
       "       pre_dispatch='2*n_jobs', refit=True, return_train_score=True,\n",
       "       scoring='neg_mean_squared_error', verbose=0)"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from sklearn.linear_model import Lasso\n",
    "params={'alpha': [0.001,0.01,0.1,1,10,100,1000]}\n",
    "las_reg = Lasso()\n",
    "lar = GridSearchCV(las_reg, params, scoring = 'neg_mean_squared_error', return_train_score=True)\n",
    "lar.fit(train_X,train_y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'mean_fit_time': array([0.03032295, 0.00932749, 0.00555555, 0.00241709, 0.00181739,\n",
       "        0.00114353, 0.00109879]),\n",
       " 'std_fit_time': array([2.60427456e-02, 7.44041457e-04, 1.56893136e-03, 5.18896809e-04,\n",
       "        4.91762901e-04, 2.74711824e-05, 8.68911343e-06]),\n",
       " 'mean_score_time': array([0.0011758 , 0.00034809, 0.00039752, 0.00026766, 0.00064468,\n",
       "        0.00071534, 0.00032258]),\n",
       " 'std_score_time': array([9.99804691e-04, 8.54743076e-05, 1.20698894e-04, 1.39243163e-05,\n",
       "        4.90202630e-04, 6.12283926e-04, 3.40552100e-05]),\n",
       " 'param_alpha': masked_array(data=[0.001, 0.01, 0.1, 1, 10, 100, 1000],\n",
       "              mask=[False, False, False, False, False, False, False],\n",
       "        fill_value='?',\n",
       "             dtype=object),\n",
       " 'params': [{'alpha': 0.001},\n",
       "  {'alpha': 0.01},\n",
       "  {'alpha': 0.1},\n",
       "  {'alpha': 1},\n",
       "  {'alpha': 10},\n",
       "  {'alpha': 100},\n",
       "  {'alpha': 1000}],\n",
       " 'split0_test_score': array([ -661587.80326205,  -660078.36648422,  -646185.62474141,\n",
       "         -637328.45708433,  -669551.03832587,  -999059.16907099,\n",
       "        -3641971.33828416]),\n",
       " 'split1_test_score': array([ -657476.37415821,  -657463.56529996,  -657309.3673506 ,\n",
       "         -656378.36153271,  -699856.62850013, -1355858.94117299,\n",
       "        -3843349.63562727]),\n",
       " 'split2_test_score': array([ -694068.08183943,  -694069.29491428,  -693586.41816308,\n",
       "         -688967.55847524,  -674456.67625793, -1042059.04975667,\n",
       "        -3578307.68136522]),\n",
       " 'mean_test_score': array([ -671004.66177021,  -670496.78067001,  -665646.04209175,\n",
       "         -660843.38351801,  -681299.8120293 , -1132480.28621639,\n",
       "        -3688063.83578353]),\n",
       " 'std_test_score': array([ 16352.94787278,  16659.78445244,  20223.47295162,  21308.28929289,\n",
       "         13290.13751397, 159125.88445621, 112972.39334188]),\n",
       " 'rank_test_score': array([4, 3, 2, 1, 5, 6, 7], dtype=int32),\n",
       " 'split0_train_score': array([ -556201.6535501 ,  -556204.62210509,  -556383.30473278,\n",
       "         -557411.48570267,  -600215.94630098, -1078609.29918201,\n",
       "        -3689353.65786639]),\n",
       " 'split1_train_score': array([ -539234.72270723,  -539234.80875032,  -539241.07276104,\n",
       "         -539851.21057257,  -582851.09105777, -1077301.00781639,\n",
       "        -3584693.1542086 ]),\n",
       " 'split2_train_score': array([ -532179.08525587,  -532179.15727453,  -532184.49153926,\n",
       "         -532711.90065817,  -571651.41521365, -1104036.77771158,\n",
       "        -3709522.85664694]),\n",
       " 'mean_train_score': array([ -542538.48717107,  -542539.52937665,  -542602.95634436,\n",
       "         -543324.86564447,  -584906.15085747, -1086649.02823666,\n",
       "        -3661189.88957398]),\n",
       " 'std_train_score': array([10081.57036667, 10082.87736362, 10161.11332203, 10378.40980149,\n",
       "        11751.61158154, 12306.59119947, 54714.48337221])}"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "lar.cv_results_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'alpha': 1}"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "lar.best_params_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "-660843.3835180105"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "lar.best_score_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Lasso(alpha=1, copy_X=True, fit_intercept=True, max_iter=1000,\n",
       "   normalize=False, positive=False, precompute=False, random_state=None,\n",
       "   selection='cyclic', tol=0.0001, warm_start=False)"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "lar.best_estimator_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [],
   "source": [
    "pred_y = lar.predict(test_X)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [],
   "source": [
    "lar_result = math.sqrt(mean_squared_error(test_y, pred_y))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 三种模型得到的各特征的系数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [],
   "source": [
    "lr_coef = lr.coef_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([-1.96086971e+16, -1.96086971e+16, -1.96086971e+16, -1.96086971e+16,\n",
       "        1.37461235e+15,  1.37461235e+15,  1.37461235e+15,  1.37461235e+15,\n",
       "        1.37461235e+15,  1.37461235e+15,  1.37461235e+15,  1.37461235e+15,\n",
       "        1.37461235e+15,  1.37461235e+15,  1.37461235e+15,  1.37461235e+15,\n",
       "       -2.10715069e+15, -2.10715069e+15, -2.10715069e+15, -8.59787045e+13,\n",
       "       -6.80911679e+14, -6.80911679e+14, -6.80911679e+14, -6.80911679e+14,\n",
       "       -6.80911679e+14, -8.59787045e+13,  3.03951955e+03,  9.28041589e+02,\n",
       "       -1.39952039e+03, -1.24388904e+03,  5.94932975e+14,  5.94932975e+14,\n",
       "        1.97865588e+03])"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "lr_coef"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [],
   "source": [
    "rr_coef = rr.best_estimator_.coef_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([-8.60472386e+02,  1.06933392e+02, -8.96525330e+00,  7.62504248e+02,\n",
       "       -3.19576474e+02, -2.13302704e+02,  3.09236065e+02,  4.90820736e+01,\n",
       "        3.54328994e+02,  1.94224768e+02, -3.63383599e+02,  4.74378598e+01,\n",
       "        6.65453534e+02,  1.88154339e+02, -4.50430114e+02, -4.61224743e+02,\n",
       "        7.99392206e+02,  2.50738083e+02, -1.05013029e+03, -1.93056980e+02,\n",
       "       -1.17975877e+02, -8.07909448e+01,  2.16395477e+01,  1.63370026e+00,\n",
       "        7.99219904e+01,  2.88628563e+02,  1.97166659e+03,  1.61595174e+03,\n",
       "       -1.15390765e+03, -1.10539679e+03, -3.05632764e+02,  2.10061180e+02,\n",
       "        1.98715547e+03])"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rr_coef"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [],
   "source": [
    "lar_coef = lar.best_estimator_.coef_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([-1.02604006e+03,  0.00000000e+00, -1.70395408e+02,  6.09108261e+02,\n",
       "       -1.97642229e+02, -1.11267653e+02,  3.36220333e+02,  4.44992164e+00,\n",
       "        2.64556237e+02,  6.20804948e+01, -4.75556798e+02, -4.36155295e+01,\n",
       "        6.19542616e+02,  1.91785073e+02, -3.89494204e+02, -3.74680144e+02,\n",
       "        5.20285471e+02, -0.00000000e+00, -1.30330186e+03, -3.84591748e+02,\n",
       "       -1.03145418e+02, -7.27185395e+01,  1.91559281e+01, -0.00000000e+00,\n",
       "        7.23022160e+01,  8.73276366e+01,  2.99807297e+03,  9.11935959e+02,\n",
       "       -1.30502224e+03, -1.18430067e+03, -5.16715751e+02,  1.77743158e+00,\n",
       "        1.98249001e+03])"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "lar_coef"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(796.6373973503521, 794.0773640748932, 796.1315124655486)"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "lr_result, rr_result, lar_result"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 结论"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "最小二乘线性回归、岭回归、Lasso三种回归模型\n",
    "\n",
    "比较它们的特征系数可以发现\n",
    "最小二乘线性回归的模型复杂度最高，因为各特征系数差了14个数量级（9.28041589e+02 与 -1.96086971e+16）\n",
    "而岭回归与Lasso模型复杂度都相对较低，各系数最多差2个数量级，可以注意到Lasso的33个系数中有3个为0.\n",
    "验证了“当正则参数取合适值时，L1正则使得有些线性回归系数为0，得到稀疏模型。”\n",
    "\n",
    "对于本问题的性能顺序为：岭回归 > Lasso > 最小二乘线性回归"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 原因解释"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "最小二乘线性回归没有加入正则项，对训练结果的评估只追求最小的真值与预测值的残差平方和，复杂的模型会是的残差平方和趋向减小\n",
    "所以得到很复杂的模型\n",
    "\n",
    "岭回归和Lasso加入了正则项，根据GridSearchCV的计算，alpha=1时模型的性能最好。\n",
    "岭回归最好模型是使得残差平方和加上各系数平方和最小\n",
    "Lasso最好模型是使得残差平方和加上各系数绝对值最小\n",
    "所以不会出现很复杂的系数\n",
    "\n",
    "对于性能的排序，最小二乘线性回归对于训练样本有一定程度的过拟合，所以虽然模型复杂，但预测结果并不如模型简单的其他两种\n",
    "岭回归模型的性能好于Lasso，可能是因为本样例各特征与目标变量都有一定的相关性，Lasso中3个为0的系数对模型的解释并不完全正确。"
   ]
  }
 ],
 "metadata": {
  "celltoolbar": "Raw Cell Format",
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
